package com.bigzero.workflow.modules.system.controller;

import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.common.utils.PageUtil;
import com.bigzero.workflow.modules.system.dto.MenuDto;
import com.bigzero.workflow.modules.system.dto.UserDto;
import com.bigzero.workflow.modules.system.entity.UserEntity;
import com.bigzero.workflow.modules.system.query.UserQuery;
import com.bigzero.workflow.modules.system.service.UserService;
import com.bigzero.workflow.modules.system.vo.UserVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理前端控制器
 * @author fanxinxiong
 * @since 2025-02-11
 */
@RestController
@RequestMapping("/system/user")
@Tag(name = "系统管理-用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 列表
     * @param query 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "分页列表")
    @Parameters({
        @Parameter(name = "pageNo", description = "当前页", schema = @Schema(type = "Integer"), example="1", required = true),
        @Parameter(name = "pageSize", description = "每页显示记录数", schema = @Schema(type = "Integer"), example="10", required = true)
    })
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应", content = @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = UserVo.class))))
    })
    @PreAuthorize("hasAuthority('system:user:list')")
    @GetMapping("list")
    public TableDataInfo list(UserQuery query) {
        PageUtil.startPage();
        List<UserVo> list = userService.queryPage(query);
        return PageUtil.getDataTable(list);
    }

    /**
     * 详情
     *
     * @param userId 用户id
     * @return 用户详情
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "详情")
    @Parameters({
        @Parameter(name = "userId", description = "用户id", example = "gf45e0320d93832c90dd3ed323129dd3", required = true)
    })
    @GetMapping("info/{userId}")
    public R<UserVo> info(@PathVariable String userId) {
        UserVo vo = userService.getVoById(userId);
        return R.ok(vo);
    }

    /**
     * 表单校验
     * @param userDto
     */
    public void formValidate(UserDto userDto) {
        if(StringUtils.isBlank(userDto.getUsername())) {
            throw new WorkflowException("失败:用户名不能为空");
        }
        if(StringUtils.isBlank(userDto.getAccount())) {
            throw new WorkflowException("失败:账号不能为空");
        }
        if(StringUtils.isBlank(userDto.getPassword())) {
            throw new WorkflowException("失败:密码不能为空");
        }
        if(StringUtils.isBlank(userDto.getEmail())) {
            throw new WorkflowException("失败:邮箱不能为空");
        }
        if(StringUtils.isBlank(userDto.getMobile())) {
            throw new WorkflowException("失败:手机号不能为空");
        }
        if(userDto.getDeptId() == null) {
            throw new WorkflowException("失败:部门不能为空");
        }
    }

    /**
     * 保存
     *
     * @param userDto 数据
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:user:insert') or hasAuthority('system:user:update')")
    @PostMapping("save")
    public R<String> save(@RequestBody UserDto userDto) {
        //表单校验
        formValidate(userDto);
        userService.insertOrUpdate(userDto);
        return R.ok();
    }

    /**
     * 删除
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:user:delete')")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String id) {
        if(StringUtils.isBlank(id)) {
            throw new WorkflowException("失败:部门主键不能为空");
        }
        userService.deleteById(id);
        return R.ok();
    }

}
